package Named::Pipes;

use 5.008008;
use strict;
use warnings;

use POSIX qw{EWOULDBLOCK mkfifo};
use File::Spec;
use File::Temp qw/ tempfile tempdir /;
require Exporter;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use Named::Pipes ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
	
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	
);

our $VERSION = '0.01';


# Preloaded methods go here.

sub open{
   my $class = shift;

   my ($metoapp, $apptome, $err, $command) = @_;
   my ($fmetoapp, $fapptome, $ferr);

   my $tmp = File::Spec->tmpdir();
   my $dir = tempdir( "$tmp/PIPESXXXX", CLEANUP => 1 );
   $metoapp = "$dir/$metoapp";
   $apptome = "$dir/$apptome";
   $err = "$dir/$err";

   mkfifo $metoapp, 0755;
   mkfifo $apptome, 0755;
   mkfifo $err, 0755;

   if(!(my $pid = fork())){
      exec "$command < $metoapp > $apptome 2> $err";
   }
   open($fmetoapp,"> $metoapp");
   open($fapptome,"$apptome");
   open($ferr,"$err");
   return ($fmetoapp,$fapptome,$ferr);
}


1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

Named::Pipes - Libreria que nos permite crear pipes con nombre

=head1 SYNOPSIS

  use Named::Pipes;
  my ($fin,$fout,$ferr) = Named::Pipes->open($in,$out,$err,$command);

=head1 DESCRIPTION

   Existe una rutina llamada open que crea 3 pipes con nombre y si se ha especificado un comando
   se crea un proceso hijo al cual se redirige la entrada y salida para poder manipular su funcionamiento

=head2 EXPORT

None by default.



=head1 SEE ALSO

   Nada

=head1 AUTHOR

ABEL GARCIA DORTA, E<lt>alu3352@E<gt>

=head1 COPYRIGHT AND LICENSE

GLPv3 2009 by GARCIA DORTA

This library is open software; you can redistribute it and/or modify
it under the same terms as GPLv3.

=cut
